﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Bike.Biz;
using Bike.Repository;
using Bike.Repository.Models;
using System.Collections;

namespace Bike.Forms.Products
{

    public partial class AddProductForm : BikeForm
    {
        IEnumerable<Category> categories;
        Category selectedCategory;
        Product editing;
        bool isEdit;
        public AddProductForm(): base()
        {
            InitializeComponent();
            
        }

        private void InitializeData()
        {
            CategoryService service = new CategoryService();

            categories = service.GetAllItems();
            UIHelper.DisplayCategory(categories, ddlCategories);
        }

        private void bntClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddProductForm_Load(object sender, EventArgs e)
        {
            InitializeData();
        }

        private void bntAddProduct_Click(object sender, EventArgs e)
        {
            ProductService service = new ProductService();
            Product product = GetProduct();
            if (!isEdit)
            {
                product = service.Add(product);

                base.ShowInfomationBox("Thêm mới sản phẩm thành công.", "Thêm Sản Phẩm");
            }
            else
            {
                service.UpdateChanges();
                editing = null;
                isEdit = false;
            }
            ResetForm();
        }

        private void ResetForm()
        {
            if (editing == null)
            {
                isEdit = false;
                bntAddProduct.Text = "Thêm Mới";
            }
            txtProductName.Text = "";
        }

        private Product GetProduct()
        {
            int catId = Convert.ToInt32(ddlCategories.SelectedItem.Value);

            bool isAccessoried = catId == SessionManager.Configuration.AccessoriesCategoryId;
            if (editing == null)
            {
                return new Product()
                {
                    Name = txtProductName.Text.Trim(),
                    Status = ProductStatuses.Normal.ToString(),
                    Type = isAccessoried ? ProductTypes.Accessories.ToString() : (radResell.IsChecked ? ProductTypes.Resell.ToString() : ProductTypes.SelfProduced.ToString()),
                    CategoryId = Convert.ToInt32(ddlCategories.SelectedItem.Value)


                };
            }
             editing.Name = txtProductName.Text.Trim();
             editing.Type = isAccessoried ? ProductTypes.Accessories.ToString() : (radResell.IsChecked ? ProductTypes.Resell.ToString() : ProductTypes.SelfProduced.ToString());
            return editing;
              
        }

        private void ddlCategories_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if(UIHelper.IsAccessoriesCategory(ddlCategories.SelectedItem.Value))
            {
                radResell.Enabled = false;
                radSelf.Enabled = false;
            }
            else
            {
                radResell.Enabled = true;
                radSelf.Enabled = true;
            }

            UpdateProductGridView();
        }

        private void UpdateProductGridView()
        {
            selectedCategory = categories.ElementAt(ddlCategories.SelectedIndex);
            grvProducts.DataSource = selectedCategory.Products;
            
        }

        private void grvProducts_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            ShowUpdateProduct(Convert.ToInt32(grvProducts.CurrentRow.Cells[0].Value));
            
        }

        private void ShowUpdateProduct(int id)
        {
            ResetForm();
            editing = selectedCategory.Products.Where(p => p.Id == id).First();
            txtProductName.Text = editing.Name;
            ProductTypes type = (ProductTypes)Enum.Parse(typeof(ProductTypes), editing.Type);
            radResell.IsChecked = type== ProductTypes.Resell;
            bntAddProduct.Text = "Cập Nhật";
            isEdit = true;
        }

        

    }
}
